<template>
  <a-config-provider :locale="locale">
    <div id="app">
      <router-view/>
    </div>
  </a-config-provider>
</template>

<script>
import { domTitle, setDocumentTitle } from '@/utils/domUtil'
import { i18nRender } from '@/locales'
import { mapMutations, mapState } from 'vuex';
import { io } from "socket.io-client";
import { ACCESS_TOKEN } from '@/store/mutation-types'
import storage from 'store'
export default {
  data () {
    return {
    }
  },
  watch: {
    isWebSoket:function(newValue,oldValue){
      this.initsoket()
    }
  },
  mounted() {
    this.initsoket()
  },
  computed: {
    ...mapState("websoket", ['isWebSoket']),
    locale () {
      // 只是为了切换语言时，更新标题
      const { title } = this.$route.meta
      title && (setDocumentTitle(`${i18nRender(title)} - ${domTitle}`))

      return this.$i18n.getLocaleMessage(this.$store.getters.lang).antLocale
    },

  },
  methods:{
    initsoket(){
      if(this.isWebSoket){
        this.createWebSoket&&this.createWebSoket();
      }else{
        window.socket=null;
      }
    },
    createWebSoket(){
      const token = storage.get(ACCESS_TOKEN);
      let url='ws://121.5.35.75:8080/imserver/'+token;
      if (typeof (WebSocket) == "undefined") {
        console.log("遗憾：您的浏览器不支持WebSocket");
      } else {
        console.log("恭喜：您的浏览器支持WebSocket");
        //实现化WebSocket对象
        //指定要连接的服务器地址与端口建立连接
        //注意ws、wss使用不同的端口。我使用自签名的证书测试，
        //无法使用wss，浏览器打开WebSocket时报错
        //ws对应http、wss对应https。
        window.socket = new WebSocket(url);
        //连接打开事件
        socket.onopen = function () {
          console.log("Socket 已打开");
//          socket.send("消息发送测试(From Client)");
        };
        //收到消息事件
        window.socket.onmessage = function (msg) {
          console.log(msg);

        };
        //连接关闭事件
        window.socket.onclose = function () {
          console.log("Socket已关闭");
        };
        //发生了错误事件
        window.socket.onerror = function () {
          alert("Socket发生了错误");
        }
        //窗口关闭时，关闭连接
        window.unload = function () {
          window.socket.close();
        };

      }
    }
  }
}
</script>
